package com.xuan.temper.controller;

import com.xuan.temper.entity.Date;
import com.xuan.temper.entity.ResponeBean;
import com.xuan.temper.service.DateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;

/**
 * @创建人： king xuan
 * @创建时间：2020/9/1
 * @描述：格式换年并写入数据库
 */
@RestController
@RequestMapping("/fomart")
public class FormatDateController {
    @Autowired
    private DateService dateService;

    @RequestMapping("/year")
    public ResponeBean sunSatdayMeth(@RequestParam("year") String year) throws ParseException {
//        String year="2020";
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        Calendar c = Calendar.getInstance();
        c.set(Integer.parseInt(year), 0, 1);
        Calendar c2 = Calendar.getInstance();
        c2.set(Integer.parseInt(year) + 1, 0, 1);
        String []arr = {"星期日","星期一","星期二","星期三","星期四","星期五","星期六"};
        SimpleDateFormat ft = new SimpleDateFormat("yyy-MM-dd");
        int count = 0;
        while (c.compareTo(c2) < 0) {
            Date date = new Date();
            if (c.get(Calendar.DAY_OF_WEEK) == Calendar.SATURDAY || c.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY) {
                System.out.println(c.get(Calendar.YEAR) + "-" + (c.get(Calendar.MONTH) + 1) + "-"
                        + c.get(Calendar.DAY_OF_MONTH)+" "+arr[(c.get(Calendar.DAY_OF_WEEK))-1]);
                String day =c.get(Calendar.YEAR) +"-"+ (c.get(Calendar.MONTH) + 1) +"-"+ c.get(Calendar.DAY_OF_MONTH);
                java.util.Date parse = format.parse(day);
                String s = format.format(parse);
                date.setDate(s);
                date.setWeek(arr[(c.get(Calendar.DAY_OF_WEEK))-1]);
                date.setHoliday(true);
                Boolean insert = dateService.insert(date);
                count++;
            }else {
                System.out.println(c.get(Calendar.YEAR) + "-" + (c.get(Calendar.MONTH) + 1) + "-"
                        + c.get(Calendar.DAY_OF_MONTH)+" "+arr[(c.get(Calendar.DAY_OF_WEEK))-1]);
                String day =c.get(Calendar.YEAR) +"-"+ (c.get(Calendar.MONTH) + 1) +"-"+ c.get(Calendar.DAY_OF_MONTH);
                java.util.Date parse = format.parse(day);
                String s = format.format(parse);
                date.setDate(s);
                date.setWeek(arr[(c.get(Calendar.DAY_OF_WEEK))-1]);
                date.setHoliday(false);
                Boolean insert = dateService.insert(date);
            }
            // 日期+1
            c.add(Calendar.DATE, 1);
        }
        System.out.println("共" + count + "条记录");
        return new ResponeBean("200","success","计算完成，日期格式成功！！！");
    }
}
